<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>数组(array) | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="c#学习笔记" />
  
  
  
  
  <meta name="description" content="数组(array)">
<meta name="keywords" content="c#学习笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="数组(array)">
<meta property="og:url" content="http://www.hais2.com/2015/06/07/c#-array/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="数组(array)">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp89.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp90.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp91.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp92.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp93.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp94.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp95.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp96.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp97.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp98.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp99.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp100.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp101.png">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数组(array)">
<meta name="twitter:description" content="数组(array)">
<meta name="twitter:image" content="http://www.hais2.com/image/cSharp/cSharp89.png">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-c#-array" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      数组(array)
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2015/06/07/c#-array/" class="article-date">
	  <time datetime="2015-06-06T16:20:12.000Z" itemprop="datePublished">June 7, 2015</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><p>　　<strong>数组实际上是由一个变量名称表示的一组同类型的数据元素</strong>，每一个元素都可以通过变量名称和一个或多个方括号中的索引名称来访问，例如：<code>MyArray[1]</code>。</p>
<h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><p>　　■ 元素：数组中<strong>独立的数据项</strong>被称为元素。数组的所有元素必须是相同类型的，或继承自相同的类型。<br>　　■ 秩/维度：数组可以有任何为正数的维度数(也就是几维数组的意思，例如二维数组的秩等于2)，数组的维度数称为秩(rank)。<br>　　■ 维度长度：数组的每一个维度有一个长度，就是这个方向的位置个数。<br>　　■ 数组长度：数组的所有维度中的元素的总和称为数组的长度。<br><a id="more"></a><br>　　c#数组的要点：<br>　　■ 数组一旦被创建大小就固定了。c#不支持动态数组。<br>　　■ 数组索引号是从0开始的。<br><img src="/image/cSharp/cSharp89.png" alt=""></p>
<h2 id="数组的类型"><a href="#数组的类型" class="headerlink" title="数组的类型"></a>数组的类型</h2><p>　　c#提供了两种类型的数组：<br>　　■ 一维数组可以认为是单行元素或元素向量。<br>　　■ 多维数组是由主向量中的位置组成的，每一个位置本身又是一个数组，称为<em>子数组(sub-array)</em>。子数组向量中的位置本身又是一个子数组。<br>　　　　□ 矩形数组(rectangular array)：<br>　　　　　　● 某个维度所有的子数组有相同长度的多维数组。<br>　　　　　　● 不管有多少维度，总是使用一组方括号。<code>myArray[2,4,5]</code><br>　　　　□ 交错数组(jagged array)：<br>　　　　　　● 每一个子数组都是独立数组的多维度数组。<br>　　　　　　● 可以有不同长度的子数组。<br>　　　　　　● 为数组的每一个维度使用一对方括号。<code>jagArray[2][4][5]</code><br><img src="/image/cSharp/cSharp90.png" alt=""></p>
<h2 id="数组是对象"><a href="#数组是对象" class="headerlink" title="数组是对象"></a>数组是对象</h2><p>　　<strong>数组实例是从System.Array继承的对象</strong>。数组从BCL基类继承了很多有用的方法：<br>　　■ Rank：返回数组维度数的属性。<br>　　■ Length：返回数组的长度(数组中所有元素的个数)的属性。</p>
<p>　　<strong>数组是引用类型</strong>，数组对象本身总是存在堆上，引用存在栈或堆上。<br><img src="/image/cSharp/cSharp91.png" alt=""><br>　　<br>　　数组是引用类型，但是数组的元素可以是引用类型，也可以是值类型。<br>　　■ 如果存储的元素都是值类型，数组被称为<strong>值类型数组</strong>。<br>　　■ 如果存储在数组中的元素都是引用类型对象，数组被称为<strong>引用类型数组</strong>。<br><img src="/image/cSharp/cSharp92.png" alt=""></p>
<h2 id="一维数组和矩形数组"><a href="#一维数组和矩形数组" class="headerlink" title="一维数组和矩形数组"></a>一维数组和矩形数组</h2><h3 id="声明一维数组或矩形数组"><a href="#声明一维数组或矩形数组" class="headerlink" title="声明一维数组或矩形数组"></a>声明一维数组或矩形数组</h3><p>　　声明一维数组或矩形数组，可以在类型后使用一对方括号：<code>int[] myIntArray;</code><br>　　★ 方括号内的逗号是<strong>秩说明符</strong>，它指定了数组的维度数，没有逗号代表一维数组，有一个逗号表示二维数组……</p>
<p>　　矩形数组：<br>　　■ 可以使用任意多的秩说明符。<br>　　■ 不能在数组类型区域中放数组维度长度，秩是数组类型的一部分，而维度长度不是类型的一部分。<br>　　■ 数组声明后，维度数就固定了，但是维度长度直到数组实例化时才会被确定。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[,,] arr;    <span class="comment">//数组类型：三维整型数组</span></span><br><span class="line"><span class="keyword">int</span>[,] arr1;    <span class="comment">//数组类型：二维整型数组</span></span><br><span class="line"><span class="keyword">long</span>[,,] arr2;  <span class="comment">//数组类型：三维long数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">long</span>[<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>] secondArray;   <span class="comment">//编译错误，不允许指定维度长度。</span></span><br></pre></td></tr></table></figure></p>
<h2 id="实例化一维数组或矩形数组"><a href="#实例化一维数组或矩形数组" class="headerlink" title="实例化一维数组或矩形数组"></a>实例化一维数组或矩形数组</h2><p>　　使用<strong>数组创建表达式</strong>实例化数组，数组创建表达式由new运算符构成，后面是基类名称和一对方括号。<strong>方括号中以逗号分隔每一个维度的长度</strong>。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] arr2 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>];    <span class="comment">//四个元素的整型数组</span></span><br><span class="line">MyClass[] mcArr = <span class="keyword">new</span> MyClass[<span class="number">4</span>];   <span class="comment">//四个元素的MyClass(自定义类型)数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span>[,,] arr3 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>,<span class="number">6</span>,<span class="number">2</span>];   <span class="comment">//三维数组，数组长度是：3*6*2=36</span></span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp93.png" alt=""></p>
<h2 id="访问数组元素"><a href="#访问数组元素" class="headerlink" title="访问数组元素"></a>访问数组元素</h2><p>　　在数组中使用整型值作为索引来访问数组元素。<br>　　■ 每一个维度的索引从0开始。<br>　　■ 方括号内的索引在数组名称之后。<br><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">int[] intArr1 = new int[<span class="number">15</span>];   <span class="comment">//声明数组长度为15的一维数组</span></span><br><span class="line">intArr1[<span class="number">2</span>] = <span class="number">10</span>;               <span class="comment">//向数组的第3个元素赋值</span></span><br><span class="line">int var1 = intArr1[<span class="number">2</span>];         <span class="comment">//从数组第3个元素读取值</span></span><br><span class="line"></span><br><span class="line">int[,] intArr2 = new int[<span class="number">5</span>,<span class="number">10</span>];<span class="comment">//声明二维数组</span></span><br><span class="line">intArr2[<span class="number">2</span>,<span class="number">3</span>] = <span class="number">7</span>;              <span class="comment">//向数组赋值</span></span><br><span class="line">int var2 = intArr2[<span class="number">2</span>,<span class="number">3</span>];       <span class="comment">//从数组读取值</span></span><br></pre></td></tr></table></figure></p>
<h2 id="初始化数组"><a href="#初始化数组" class="headerlink" title="初始化数组"></a>初始化数组</h2><p>　　当数组被创建之后，数组中的每一个元素都会被自动的初始化为类型的默认值：整型默认值是0，浮点型的默认值是0.0，布尔型的默认值为false，引用类型的默认值是null。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] intArr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>];</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp94.png" alt=""></p>
<h3 id="显式初始化一维数组"><a href="#显式初始化一维数组" class="headerlink" title="显式初始化一维数组"></a>显式初始化一维数组</h3><p>　　对一维数组，我们可以在数组实例化的时候显式的设置初始值。<br>　　■ 初始值必须以逗号分隔，并封闭在一组花括号内。<br>　　■ 维度长度是可选的，因为编译器可以通过初始化值的个数来推断长度。<br><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int[] intArr = new int[] &#123;<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>&#125;;</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp95.png" alt=""></p>
<h3 id="显式初始化矩形数组"><a href="#显式初始化矩形数组" class="headerlink" title="显式初始化矩形数组"></a>显式初始化矩形数组</h3><p>　　■ 矩形数组中每一个初始值向量必须封闭在花括号内。<br>　　■ 除了初始值，每一个维度的初始化列表和组成部分必须使用逗号分隔。<br><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int[,] intArr2 = new int[,] &#123;&#123;<span class="number">10</span>,<span class="number">1</span>&#125;,&#123;<span class="number">2</span>,<span class="number">10</span>&#125;,&#123;<span class="number">11</span>,<span class="number">9</span>&#125;&#125;;</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp96.png" alt=""></p>
<h3 id="快捷语法"><a href="#快捷语法" class="headerlink" title="快捷语法"></a>快捷语法</h3><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int[] arr1 = new int[]&#123;<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>&#125;;</span><br><span class="line">int[] arr1 = &#123;<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>&#125;;</span><br><span class="line"></span><br><span class="line">int[,] arr2 = new int[<span class="number">2</span>,<span class="number">3</span>]&#123;&#123;<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>&#125;&#125;;</span><br><span class="line">int[,] arr2 = &#123;&#123;<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>&#125;&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="交错数组"><a href="#交错数组" class="headerlink" title="交错数组"></a>交错数组</h2><p>　　交错数组是数组的数组。交错数组的子数组可以有不同数目的元素。<br><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//声明并创建顶层数组</span><br><span class="line">int[<span class="string"></span>][<span class="symbol"></span>] jagArr = new int[<span class="string">3</span>][<span class="symbol"></span>];  //声明三个int数组的数组</span><br><span class="line">//声明并创建子数组</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp97.png" alt=""></p>
<h3 id="声明交错数组"><a href="#声明交错数组" class="headerlink" title="声明交错数组"></a>声明交错数组</h3><p>　　交错数组的声明语法要求每一个维度都有一对独立的方括号。交错数组变量声明中的方括号数决定了数组的秩。<br>　　■ 交错数组可以有任何大于1的维度。<br><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">int[][] SomeArr;     //秩等于2</span><br><span class="line">int[<span class="string"></span>][<span class="symbol"></span>][<span class="string"></span>] OtherArr;  //秩等于3</span><br></pre></td></tr></table></figure></p>
<p>　　不能再声明语句中初始化最高级别数组之外的数组<br><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">int[<span class="string"></span>][<span class="symbol"></span>] jagArr = new int[<span class="string">3</span>][<span class="symbol"></span>];   //三个子数组的顶层数组</span><br><span class="line">int[<span class="string"></span>][<span class="symbol"></span>] jagArr = new int[<span class="string">3</span>][<span class="symbol">4</span>];  //编译错误，不允许初始化顶层以外的数组</span><br></pre></td></tr></table></figure></p>
<h3 id="实例化交错数组"><a href="#实例化交错数组" class="headerlink" title="实例化交错数组"></a>实例化交错数组</h3><p>　　由于交错数组是独立数组的数组，所以每一个数组都必须独立创建：<br>　　① 首先实例化顶层数组。<br>　　② 其次分别实例化每一个子数组，把新建子数组的引用赋值给包含它们的数组的合适元素。<br><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int[<span class="string"></span>][<span class="symbol"></span>] Arr = new int[<span class="string">3</span>][<span class="symbol"></span>];              //1、实例化顶层数组</span><br><span class="line"></span><br><span class="line">Arr[0] = new int[] &#123;10,20,30&#125;;           //2、实例化子数组</span><br><span class="line">Arr[1] = new int[] &#123;40,50,60,70&#125;;        //3、实例化子数组</span><br><span class="line">Arr[2] = new int[] &#123;80,90,100,110,120&#125;;  //4、实例化子数组</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp98.png" alt=""></p>
<h3 id="交错数组中的子数组"><a href="#交错数组中的子数组" class="headerlink" title="交错数组中的子数组"></a>交错数组中的子数组</h3><p>　　交错数组中的子数组本身就是数组，因此交错数组中也可能有矩形数组。<br><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int[][,] Arr = new int[<span class="number">3</span>][,];   <span class="comment">//实例化带有三个二维数组的交错数组</span></span><br><span class="line"></span><br><span class="line">Arr[<span class="number">0</span>] = new int[,] &#123;&#123;<span class="number">10</span>,<span class="number">20</span>&#125;,&#123;<span class="number">100</span>,<span class="number">200</span>&#125;&#125;;</span><br><span class="line">Arr[<span class="number">1</span>] = new int[,] &#123;&#123;<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>&#125;,&#123;<span class="number">300</span>,<span class="number">400</span>,<span class="number">500</span>&#125;&#125;;</span><br><span class="line">Arr[<span class="number">2</span>] = new int[,] &#123;&#123;<span class="number">60</span>,<span class="number">70</span>,<span class="number">80</span>,<span class="number">90</span>&#125;,&#123;<span class="number">600</span>,<span class="number">700</span>,<span class="number">800</span>,<span class="number">900</span>&#125;&#125;;</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp99.png" alt=""></p>
<h2 id="foreach语句在数组的使用"><a href="#foreach语句在数组的使用" class="headerlink" title="foreach语句在数组的使用"></a>foreach语句在数组的使用</h2><p>　　■ 迭代变量是临时的，只读的，并且和数组中元素的类型相同。foreach使用迭代变量来连续表示数组中的每一个元素。<br>　　■ foreach语句的工作方式：<br>　　　　□ 从数组的第一个元素开始遍历并把它赋值给迭代变量。<br>　　　　□ 然后执行语句主体(以大括号包裹的内容)，在主体中，把当前遍历到的一个数组元素使用迭代变量表示，该元素是只读的。<br>　　　　□ 在主体执行之后，foreach语句选择下一个数组元素，并重复这些过程。</p>
<h3 id="迭代变量是只读的"><a href="#迭代变量是只读的" class="headerlink" title="迭代变量是只读的"></a>迭代变量是只读的</h3><p>　　因为迭代变量是只读的，所以在foreach中不能实现修改数组元素值的功能。这个只读属性对于值类型数组和引用类型数组来说效果是不一样的。<br>　　★ 对于值类型数组和引用类型数组来说，迭代变量都是不能被改变的。但是例外的是，引用类型存储的是数据的引用，但是数据本身并没有和迭代变量有联系，所以我们可以在foreach中修改数据本身。</p>
<p><strong>值类型数组</strong>：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] arr1 = &#123;<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">13</span>&#125;;</span><br><span class="line"><span class="keyword">foreach</span>(<span class="keyword">var</span> item <span class="keyword">in</span> arr1)</span><br><span class="line">&#123;</span><br><span class="line">    item++;    <span class="comment">//错误！！编译时会抛出错误</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>引用类型数组</strong>：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title">MyClass</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> MyField = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        MyClass[] mc = <span class="keyword">new</span> MyClass[<span class="number">4</span>];  <span class="comment">//创建引用类型数组</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>;i&lt;<span class="number">4</span>;i++)   <span class="comment">//初始化数组</span></span><br><span class="line">        &#123;</span><br><span class="line">             mc[i] = <span class="keyword">new</span> MyClass();  <span class="comment">//创建类对象</span></span><br><span class="line">             mc[i].MyField = i;      </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">foreach</span>(<span class="keyword">var</span> item <span class="keyword">in</span> mc)</span><br><span class="line">        &#123;</span><br><span class="line">            item.MyFiled+=<span class="number">10</span>;     <span class="comment">//遍历修改数据本身，引用没有发生变化</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">foreach</span>(<span class="keyword">var</span> item <span class="keyword">in</span> mc)</span><br><span class="line">        &#123;</span><br><span class="line">            Console.WriteLine(item);</span><br><span class="line">        &#125;</span><br><span class="line">        Console.ReadKey();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="数组协变"><a href="#数组协变" class="headerlink" title="数组协变"></a>数组协变</h2><p>　　在下面情况下，即使某一个对象和数组的类型不相同，我们也可以把它赋值给数组元素：<br>　　■ 数组是引用类型数组。<br>　　■ 在赋值的对象或者类型之间有隐式转换或强制转换。</p>
<p>　　★ 由于派生类和基类之间默认是含有隐式的转换的，所以总是可以将一个派生类的对象赋值给基类数组元素。<br><img src="/image/cSharp/cSharp100.png" alt=""><br>　　 👉 值类型数组没有协变。</p>
<h2 id="Array继承的有用成员"><a href="#Array继承的有用成员" class="headerlink" title="Array继承的有用成员"></a>Array继承的有用成员</h2><p>　　c#数组从System.Array类继承，有很多有用的属性和方法：<br><img src="/image/cSharp/cSharp101.png" alt=""></p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/c/">c#</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/c-学习笔记/">c#学习笔记</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/06/18/c#-reflection/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          反射(reflection)
        
      </div>
    </a>
  
  
    <a href="/2015/06/04/c#-enumerate/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">枚举(enumerate)</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#数组"><span class="nav-number">1.</span> <span class="nav-text">数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#定义"><span class="nav-number">1.1.</span> <span class="nav-text">定义</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组的类型"><span class="nav-number">2.</span> <span class="nav-text">数组的类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组是对象"><span class="nav-number">3.</span> <span class="nav-text">数组是对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一维数组和矩形数组"><span class="nav-number">4.</span> <span class="nav-text">一维数组和矩形数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#声明一维数组或矩形数组"><span class="nav-number">4.1.</span> <span class="nav-text">声明一维数组或矩形数组</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实例化一维数组或矩形数组"><span class="nav-number">5.</span> <span class="nav-text">实例化一维数组或矩形数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#访问数组元素"><span class="nav-number">6.</span> <span class="nav-text">访问数组元素</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#初始化数组"><span class="nav-number">7.</span> <span class="nav-text">初始化数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#显式初始化一维数组"><span class="nav-number">7.1.</span> <span class="nav-text">显式初始化一维数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#显式初始化矩形数组"><span class="nav-number">7.2.</span> <span class="nav-text">显式初始化矩形数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#快捷语法"><span class="nav-number">7.3.</span> <span class="nav-text">快捷语法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#交错数组"><span class="nav-number">8.</span> <span class="nav-text">交错数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#声明交错数组"><span class="nav-number">8.1.</span> <span class="nav-text">声明交错数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例化交错数组"><span class="nav-number">8.2.</span> <span class="nav-text">实例化交错数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#交错数组中的子数组"><span class="nav-number">8.3.</span> <span class="nav-text">交错数组中的子数组</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#foreach语句在数组的使用"><span class="nav-number">9.</span> <span class="nav-text">foreach语句在数组的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#迭代变量是只读的"><span class="nav-number">9.1.</span> <span class="nav-text">迭代变量是只读的</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组协变"><span class="nav-number">10.</span> <span class="nav-text">数组协变</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Array继承的有用成员"><span class="nav-number">11.</span> <span class="nav-text">Array继承的有用成员</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
